<template>
  <div>
    <p>
      <button @click="list(1)" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>
    <div class="row">
      <div class="col-md-6">
        <textarea id="resource-textarea" class="form-control" v-model="resourceStr" name="resource" rows="10"></textarea>
        <br>
        <button id="save-btn" type="button" class="btn btn-primary" v-on:click="save()">
          保存
        </button>
      </div>
      <div class="col-md-6">
        <ul id="tree" class="ztree"></ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  name: "system-resource",
  data: function () {
    return {
      resource: {},
      resources: [],
      resourceStr: "",
      tree: {}
    }
  },
  mounted: function () {
    let _this = this;
    _this.list();
    // sidebar激活样式方法一
    // this.$parent.activeSidebar("system-resource-sidebar")
  },
  methods: {
    /**
     * 点击【保存】
     */
    save() {
      let _this = this;

      // 保存校验
      if (Tool.isEmpty(_this.resourceStr)) {
        Toast.warning("资源不能为空");
        return;
      }

      let json = JSON.parse(_this.resourceStr);

      Loading.show()
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/resource/save', json).then((response)=>{
        Loading.hide()
        // console.log("添加资源结果：",response)
        if (response.data.success) {
          Toast.success("新增成功！");
          _this.list();
        }else {
          Toast.warning(response.data.message)
        }
      })
    },
    /**
     * 点击【删除】
     */
    del(id) {
      let _this = this;
      Confirm.show("删除后无法恢复当前数据",function () {
        Loading.show()
        _this.$ajax.delete(process.env.VUE_APP_SERVER + '/system/admin/resource/delete/' + id).then((response)=>{
          Loading.hide()
          // console.log("删除资源结果：",response)
          if (response.data.success) {
            Toast.success("删除成功！")
          }
        })
      })
    },
    /**
     * 列表查询
     */
    list() {
      let _this = this;
      Loading.show()
      _this.$ajax.get(process.env.VUE_APP_SERVER + '/system/admin/resource/load-tree').then((response)=>{
        Loading.hide()
        //console.log("查询资源列表结果：",response)
        _this.resources = response.data.content;
        _this.initTree();
      })
    },
    initTree() {
      let _this = this;
      let setting = {
        check: {
          enable: false
        },
        data: {
          simpleData: {
            idKey: "id",
            pIdKey: "parent",
            rootPId: "",
            // enable: true,
          }
        }
      };

      let zNodes = _this.resources;

      _this.tree = $.fn.zTree.init($("#tree"), setting, zNodes);

      // 展开所有节点
      _this.tree.expandAll(true);
    },
  }
}
</script>

<style scoped>

</style>